<?php
namespace UserSystem\Driver\DB;

use UserSystem\Config;
use \PDO;
use \PDOException;

/**
 * Class Db
 * 数据库连接类，基于 pdo_mysqli
 * @author wangxi
 */
class Mysql
{
    private static $_instance;

	private function __construct(){

	}

	public static function getInstance(){
		if( !isset( self::$_instance ) ) {
			$db = Config::getInstance()->get('db');

			if( is_array( $db ) && !empty( $db ) ) {
				try {
					$dsn = $db[ 'db_type' ] . ':host=' . $db[ 'host' ] .
                        ';dbname=' . $db[ 'db_name' ].';charset=utf8';

                    //TODO: check if pdo_mysql is loaded
					self::$_instance = new PDO( $dsn, $db[ 'username' ], $db[ 'password' ] );

				} catch(PDOException $e) {
					die( $e->getMessage() );
				}
			}

		}

		return self::$_instance;
	}

}